# This file is part of OrangeSpider. OrangeSpider is a meta model based
# development tool.
# 
# OrangeSpider is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# 
# OrangeSpider is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with OrangeSpider.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2009 Stefan Nikolaus <OrangeSpiderMaster@googlemail.com>

from PyQt4.QtCore import *
from PyQt4.QtGui import *

import inspect

# Script should get an environment:
# - Current project + project item self
# - ComponentManager
class ScriptEnvironment(dict):
    def __init__(self, objectMap = {}):
        self.update(objectMap)
        
class Script:
    def __init__(self, path, environ):
        self.path = path
        self.environ = environ
        
    def execute(self):
        try:
            execfile(self.path, self.environ)
        except SyntaxError, e:
            msg = "Error in %s\nLine: %d\nOffset: %d\nText: %s" % (e.filename, e.lineno, e.offset, e.text) 
            msg += str(inspect.trace())
            QMessageBox.critical(None, "Spider", msg)
        except Exception, e:
            msg = str(e)
            msg += str(inspect.trace())
            QMessageBox.critical(None, "Spider", str(e))
